package cn.saas.jingtandata;

import android.content.Context;
import android.text.TextUtils;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

import java.text.SimpleDateFormat;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        assertEquals("cn.saas.jingtandata", appContext.getPackageName());
        boolean dateString = isDateString("yyyy.MM.dd", "2021.04.24");
        System.out.println(dateString);
    }

    public static boolean isDateString(String dateValue, String dateFormat) {
        if (isBlank(dateValue)) {
            return false;
        }
        try {
            SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
            java.util.Date dd = fmt.parse(dateValue);
            if (dateValue.equals(fmt.format(dd))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isBlank(Object str) {
        if (str == null || TextUtils.isEmpty(str.toString())) {
            return true;
        }
        return false;
    }
}